package com.joyance.test_servlet.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.CollectionUtils;


public class TestServletRequest extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // ServletRequest
        System.out.println("ServletRequest================");
        Enumeration attNames = req.getAttributeNames();
        System.out.println("req.getAttributeNames[");
        while (attNames.hasMoreElements()) {
            Object attrName = attNames.nextElement();
            Object attrValue = req.getAttribute(attrName.toString());
            System.out.println(attrName + "=" + attrValue + ",");
        }
        System.out.println("]");
        // getCharacterEncoding
        System.out.println("req.getCharacterEncoding()=" + req.getCharacterEncoding());
        // getContentLength
        System.out.println("req.getContentLength()=" + req.getContentLength());
        // getContentType
        System.out.println("req.getContentType()=" + req.getContentType());
        // getProtocol
        System.out.println("req.getProtocol()=" + req.getProtocol());
        // getReader
        System.out.println("req.getReader()-------------------begin");
        // BufferedReader bufferedReader = req.getReader();
        // String line = bufferedReader.readLine();
        // while (line != null) {
        // System.out.println(line);
        // line = bufferedReader.readLine();
        // }
        System.out.println("req.getReader()-------------------end");
        // getInputStream
        System.out.println("req.getInputStream()-------------------begin");
        InputStream inputStream = req.getInputStream();
        byte[] bytes = new byte[1024];
        List<Byte> list = new ArrayList<Byte>();
        while (inputStream.read(bytes) != -1) {
            CollectionUtils.mergeArrayIntoCollection(bytes, list);
        }
        bytes = convertListToArray(list);
        String content = new String(bytes);
        System.out.println(content);
        System.out.println("req.getInputStream()-------------------end");
        // getRemoteAddr
        System.out.println("req.getRemoteAddr()=" + req.getRemoteAddr());
        // getRemoteHost
        System.out.println("req.getRemoteHost()=" + req.getRemoteHost());
        // getScheme
        System.out.println("req.getScheme()=" + req.getScheme());
        // getServerName
        System.out.println("req.getServerName()=" + req.getServerName());
        // getServerPort
        System.out.println("req.getServerPort()=" + req.getServerPort());
        // getRealPath
        System.out.println("req.getRealPath()=" + req.getRealPath("test"));
    }


    private byte[] convertListToArray(List<Byte> list) {
        byte[] bytes = new byte[list.size()];
        int index = 0;
        for (Byte b : list) {
            bytes[index++] = b;
        }
        return bytes;
    }


}
